// 纯前端版本API - 使用本地数据
const mockSongs = [
  { id: 1, name: '火', artist: '隔层', cover: 'https://picsum.photos/seed/1/100', url: 'https://www.kozco.com/tech/LRMonoPhase4.mp3' },
  { id: 2, name: '只因你太美', artist: '坤坤', cover: 'https://picsum.photos/seed/2/100', url: 'https://www.kozco.com/tech/piano2-CoolEdit.mp3' },
  { id: 3, name: '六六六', artist: '智子涵', cover: 'https://picsum.photos/seed/3/100', url: 'https://www.kozco.com/tech/organfinale.mp3' },
  { id: 4, name: '打火机', artist: 'Penny', cover: 'https://picsum.photos/seed/4/100', url: 'https://www.kozco.com/tech/LRMonoPhase4.mp3' },
  { id: 5, name: 'Star Crossing Night', artist: '徐明浩/GALI', cover: 'https://picsum.photos/seed/5/100', url: 'https://www.kozco.com/tech/piano2-CoolEdit.mp3' }
]

function apiLogin(phone){ 
  return Promise.resolve({ token: 'mock-token', user: { id: 1, name: '用户' + phone.slice(-4) } })
}

function apiSongs(){ return Promise.resolve(mockSongs) }

function apiSearch(q){
  const filtered = mockSongs.filter(song => 
    song.name.toLowerCase().includes(q.toLowerCase()) || 
    song.artist.toLowerCase().includes(q.toLowerCase())
  )
  return Promise.resolve(filtered)
}

function apiFavs(){ return Promise.resolve(wx.getStorageSync('favs') || []) }

function apiFavToggle(song){
  return new Promise((resolve)=>{
    const list = wx.getStorageSync('favs') || []
    const idx = list.findIndex(i=> i.id===song.id)
    if(idx>-1) list.splice(idx,1); else list.unshift(song)
    wx.setStorageSync('favs', list)
    resolve(list)
  })
}

function apiHistory(){ return Promise.resolve(wx.getStorageSync('playHistory') || []) }

function apiHistoryAdd(song){
  return new Promise((resolve)=>{
    const list = wx.getStorageSync('playHistory') || []
    const filtered = list.filter(i=> i.id!==song.id)
    filtered.unshift({ ...song, ts: Date.now() })
    wx.setStorageSync('playHistory', filtered.slice(0,50))
    resolve(filtered)
  })
}

module.exports = { apiLogin, apiSongs, apiSearch, apiFavs, apiFavToggle, apiHistory, apiHistoryAdd }